package com.xuecheng.media.api;


import com.xuecheng.base.model.RestResponse;
import com.xuecheng.media.model.dto.UploadFileParamsDto;
import com.xuecheng.media.service.MediaFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

/**
 * @description 媒资文件管理接口
 * @author Mr.M
 * @date 2022/9/6 11:29
 * @version 1.0
 */
 @Api(value = "大文件文件管理接口",tags = "大文件文件管理接口")
 @RestController
public class BigFilesController {


         @Autowired
         MediaFileService mediaFileService;

        @ApiOperation(value = "文件上传前检查文件")
        @PostMapping("/upload/checkfile")
        public RestResponse<Boolean> checkfile(
                @RequestParam("fileMd5") String fileMd5
        ) throws Exception {
            RestResponse<Boolean> booleanRestResponse = mediaFileService.checkFile(fileMd5);
            return booleanRestResponse;
        }


        @ApiOperation(value = "分块文件上传前的检测")
        @PostMapping("/upload/checkchunk")
        public RestResponse<Boolean> checkchunk(@RequestParam("fileMd5") String fileMd5,
                                                @RequestParam("chunk") int chunk) throws Exception {
            RestResponse<Boolean> booleanRestResponse = mediaFileService.checkChunk(fileMd5, chunk);
            return booleanRestResponse;
        }

        @ApiOperation(value = "上传分块文件")
        @PostMapping("/upload/uploadchunk")
        public RestResponse uploadchunk(@RequestParam("file") MultipartFile file,
                                        @RequestParam("fileMd5") String fileMd5,
                                        @RequestParam("chunk") int chunk) throws Exception {
            //创建临时文件
            File tempFile = File.createTempFile("minio", "temp");
            //上传的文件拷贝到临时文件
            file.transferTo(tempFile);
            String absolutePath = tempFile.getAbsolutePath();
            RestResponse<Boolean> booleanRestResponse=mediaFileService.uploadChunk(fileMd5,chunk,absolutePath);
            return booleanRestResponse;
        }

        @ApiOperation(value = "合并文件")
        @PostMapping("/upload/mergechunks")
        public RestResponse mergechunks(@RequestParam("fileMd5") String fileMd5,
                                        @RequestParam("fileName") String fileName,
                                        @RequestParam("chunkTotal") int chunkTotal) throws Exception {
            UploadFileParamsDto uploadFileParamsDto = new UploadFileParamsDto();
            Long companyId = 1232141425L;
            //文件信息对象
            uploadFileParamsDto.setFilename(fileName);
            uploadFileParamsDto.setTags("视频文件");
            uploadFileParamsDto.setFileType("001002");
            RestResponse restResponse = mediaFileService.mergeChunks(companyId, fileMd5, chunkTotal, uploadFileParamsDto);

            return restResponse;
        }


}
